<?php

namespace plugin\cms\app\controller\uc;

use core\controller\UcBase;
use Exception;
use plugin\cms\app\bll\Category as BllCategory;
use support\View;
use plugin\cms\app\bll\Model as BllModel;
use plugin\cms\app\bll\ModelField as BllModelField;
use plugin\cms\app\bll\Content as BllContent;
use app\bll\Site as BllSite;
use plugin\cms\app\bll\ContentPending as BllContentPending;
use plugin\cms\app\enum\ContentStatus;

/**
 * 投稿相关
 */
class Contribute extends UcBase
{
    public function index()
    {
        $bllCategory = new BllCategory();
        $bllModel    = new BllModel();
        $optype      = request()->get('optype', '');
        switch ($optype) {
            case 'selcontent':
                $bllContent = new BllContent();

                //获取投稿待审数量
                $contributePendingCount = $bllContent->contentCount(
                    modelTable: request()->post('tablename'),
                    uid: request()->userinfo['id'],
                    status: ContentStatus::STATUS_PENDING->value
                );

                //获取投稿已发布数量
                $contributePublishCount = $bllContent->contentCount(
                    modelTable: request()->post('tablename'),
                    uid: request()->userinfo['id'],
                    status: ContentStatus::STATUS_NORMAL->value
                );

                //获取投稿未通过数量
                $contributeUnpassCount = $bllContent->contentCount(
                    modelTable: request()->post('tablename'),
                    uid: request()->userinfo['id'],
                    status: ContentStatus::STATUS_UNPASS->value
                );


                //获取投稿
                $contributes =  $bllContent->paginate(
                    mid: request()->post('mid'),
                    status: request()->post('status'),
                    title: request()->post('title', ''),
                    uid: request()->userinfo['id'],
                    contribute: 1,
                    pagesize: 10
                );

                $bllContentPending = new BllContentPending();
                foreach ($contributes as $key => $contribute) {
                    $ancestorsCates = $bllCategory->getAncestorsAndSelf($contribute->cid, $contribute->siteid);
                    $ancestorsCatetitle = '';
                    foreach ($ancestorsCates as $acate) {
                        $ancestorsCatetitle .= $acate['title'] . ' ';
                    }
                    $contribute->ancestorsCatetitle =  $ancestorsCatetitle;

                    if (request()->post('status') == ContentStatus::STATUS_UNPASS->value) {

                        //获取退稿原因
                        $pendinginfo             = $bllContentPending->LastUnpassRecord($contribute->id, $contribute->cid, $contribute->siteid);
                        $contribute->pendinginfo = $pendinginfo;
                        $contribute->opinion     = $contribute->pendinginfo['opinion'];
                    } else {
                        $contribute->opinion = '';
                    }
                }
                return $this->success([
                    'contributePendingCount' => $contributePendingCount,
                    'contributePublishCount' => $contributePublishCount,
                    'contributeUnpassCount' => $contributeUnpassCount,
                    'items' => $contributes->items(),
                    'total' => $contributes->total(),
                ]);
                break;
        }
        //获取可投稿的站点和栏目
        $contributeCates = $bllCategory->contributeCascader();

        //获取可投稿的模型
        $bllModel->contributeModel();
        $contributeModels = $bllModel->contributeModel();

        View::assign('contributeCates', $contributeCates);
        View::assign('contributeModels', $contributeModels);





        return $this->view();
    }

    public function add()
    {
        if (is_post()) {
            try {
                $data               = request()->post();
                $siteid             = request()->get('siteid');
                $data['status']     = 0;
                $data['uid']        = request()->userinfo['id'];
                $data['contribute'] = 1;
                $bllContent  = new BllContent();
                $bllContent->add($data, $siteid);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {

            //获取可投稿的站点和栏目
            $bllCategory = new BllCategory();
            $contributecates       = $bllCategory->contributeCascader();


            $cid           = request()->get('cid', 0);
            $siteid        = request()->get('siteid', 0);

            //当前栏目层级关系
            $ancestorCate = $bllCategory->getAncestorsAndSelf($cid, $siteid);

            if (!$ancestorCate) {
                throw new Exception('栏目错误');
            }

            //当前站点信息
            $bllSite  = new BllSite();
            $siteinfo = $bllSite->find($siteid);


            $bllModel      = new BllModel();
            $modelinfo     = $bllModel->find(end($ancestorCate)['mid'], $siteid);
            $bllModelField = new BllModelField();
            $fields        = $bllModelField->selContributeField($modelinfo['id']);


            View::assign('fields', $fields);
            View::assign('siteinfo', $siteinfo);
            View::assign('ancestorCate', $ancestorCate);
            View::assign('contributecates', $contributecates);
            return $this->view();
        }
    }


    /**
     * 编辑投稿稿件
     *
     * @return mixed
     */
    public function edit()
    {
        $id     = request()->get('id', 0);
        $cid    = request()->get('cid', 0);
        $siteid = request()->get('siteid', 0);

        //内容信息
        $bllContent  = new BllContent();
        $contentinfo = $bllContent->find(id: $id, cid: $cid, siteid: $siteid);
        if (!$contentinfo) {
            throw new Exception('内容不存在');
        }

        if (is_post()) {
            try {
                $data           = request()->post();
                $data['cid']    = $cid;
                $data['status'] = ContentStatus::STATUS_PENDING->value;
                $bllContent     = new BllContent();

                $bllContent->edit(id: $id, data: $data, siteid: $siteid);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            //获取可投稿的站点和栏目
            $bllCategory     = new BllCategory();
            $contributecates = $bllCategory->contributeCascader();

            //当前栏目层级关系
            $ancestorCate = $bllCategory->getAncestorsAndSelf($cid, $siteid);

            if (!$ancestorCate) {
                throw new Exception('栏目错误');
            }

            //当前站点信息
            $bllSite  = new BllSite();
            $siteinfo = $bllSite->find($siteid);




            $bllModel      = new BllModel();
            $modelinfo     = $bllModel->find(end($ancestorCate)['mid'], $siteid);
            $bllModelField = new BllModelField();
            $fields        = $bllModelField->selContributeField($modelinfo['id']);


            View::assign('fields', $fields);
            View::assign('contentinfo', $contentinfo);
            View::assign('siteinfo', $siteinfo);
            View::assign('ancestorCate', $ancestorCate);
            View::assign('contributecates', $contributecates);
            return $this->view();
        }
    }


    /**
     * 删除投稿稿件
     *
     * @return void
     */
    public  function delete()
    {

        try {
            $id     = request()->get('id', 0);
            $cid    = request()->get('cid', 0);
            $siteid = request()->get('siteid', 0);
            $bllContent  = new BllContent();
            $contentinfo = $bllContent->find(id: $id, cid: $cid, siteid: $siteid);
            if (!$contentinfo || $contentinfo['uid'] != request()->userinfo['id']) {

                return $this->error('内容不存在');
            }
            $bllContent->delete(contentids: [$id], cid: $cid, siteid: $siteid);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
